package com.bruce.dao.impl;

import com.bruce.bean.Teacher;
import com.bruce.dao.TeacherDao;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class TeacherDaoImpl extends BaseDao implements TeacherDao {

    @Override
    public Teacher teacherLogin(Teacher t) {
        try {
            return queryRunner.query("select * from teacher where tid=? and tpassword=?",
                    new BeanHandler<Teacher>(Teacher.class),t.getTid(),t.getTpassword());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addTeacher(Teacher teacher) {
        String sql  = "insert into teacher values(?,?,?,?,?)";
        try {
            return  queryRunner.update(sql,teacher.getTid(),teacher.getTname(),teacher.getTpassword(),teacher.getTsex(),teacher.getIntroduction());
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return 0;
    }

    @Override
    public Teacher findTeacherName(String tid) {
        String sql= "select * from teacher where tid = ?";
        Teacher t = new Teacher();
        try {
             t = queryRunner.query(sql, new BeanHandler<>(Teacher.class), tid);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return t;
    }

    @Override
    public String FindIsTeacheCoursed(String cid) {
        String sql = "select tid from courseplan where cid = ?";
        String tid =null;
        try {
            tid = (String)queryRunner.query(sql, new ScalarHandler<>(), cid);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return tid;
    }
}
